package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/8/3 22:05
 * @Description:
 */
@RestController
@RequestMapping("/role")
public class RoleController {

	@Reference
	RoleService roleService;
	/**
	 *  @描述：根据角色名查找角色
	 */
	@RequestMapping("/selectByName")
	public Result selectByName(String name) {
		try {
			Role role = roleService.selectByName(name);
			return new Result(true, MessageConstant.GET_ROLE_INFO_SUCCESS,role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_ROLE_INFO_FAIL);
		}
	}
	/**
	 *  @描述：根据描述查询角色
	 */
	@RequestMapping("/selectByDesc")
	public Result selectByDesc(String desc) {
		try {
			List<Role> roles = roleService.selectByDesc(desc);
			return new Result(true, MessageConstant.GET_ROLE_INFO_SUCCESS,roles);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_ROLE_INFO_FAIL);
		}
	}
	/**
	 *  @描述：按照条件查询条数
	 */
	@RequestMapping("/selectCountByDesc")
	public Result selectCountBySex(String sex) {
		try {
			Integer count = roleService.selectCountByDesc(sex);
			return new Result(true, MessageConstant.GET_ROLE_INFO_SUCCESS,count);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_ROLE_INFO_FAIL);
		}
	}
	/**
	 *  @描述：按照总记录
	 */
	@RequestMapping("/selectAll")
	public Result selectAll() {
		try {
			List<Role> roles = roleService.selectAll();
			return new Result(true, MessageConstant.GET_ROLE_INFO_SUCCESS,roles);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_ROLE_INFO_FAIL);
		}
	}
	/**
	 *  @描述：按照总记录
	 */
	@RequestMapping("/selectById")
	public Result selectById(Integer id) {
		try {
			Role role = roleService.selectById(id);
			return new Result(true, MessageConstant.GET_ROLE_INFO_SUCCESS,role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_ROLE_INFO_FAIL);
		}
	}
	/**
	 *  @描述：新增角色(需要插入所有字段)
	 */
	@RequestMapping("/insert")
	public Result insert(@RequestBody Role role) {
		try {
			roleService.insert(role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.ADD_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
	}

	/**
	 *  @描述：新增角色(只插入不为空的字段)
	 */
	@RequestMapping("/insertSelective")
	public Result insertSelective(@RequestBody Role role) {
		try {
			roleService.insertSelective(role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.ADD_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
	}
	/**
	 *  @描述：按照条件删除角色
	 */
	@RequestMapping("/delete")
	public Result delete(String test) {
		try {
			roleService.delete(test);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
	}
	/**
	 *  @描述：按照id删除角色
	 */
	@RequestMapping("/deleteById")
	public Result deleteById(Integer id) {
		try {
			roleService.deleteById(id);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
	}
	/**
	 *  @描述：更新角色（全字段）
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody Role role) {
		try {
			roleService.update(role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
	}
	/**
	 *  @描述：更新角色（非空部分）
	 */
	@RequestMapping("/updateSelective")
	public Object updateSelective(@RequestBody Role role) {
		try {
			roleService.updateSelective(role);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
		}
		return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
	}
}
